<template>
  <van-pull-refresh v-model="refreshing" @refresh="onRefresh">
    <van-list
      v-model="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad"
      style="margin-top: 50px"
    >
      <van-cell
        v-for="item in data"
        :key="item.id"
        @click="$router.push('/detail/' + item.id)"
      >
        <HmItem :item="item"></HmItem>
      </van-cell>
    </van-list>
  </van-pull-refresh>
</template>

<script>
import axios from "axios";
export default {
  name: "HmRecommend",
  components: {},
  methods: {
    fn() {
      // axios();
    },
    onLoad() {
      // 异步更新数据
      // setTimeout 仅做示例，真实场景中一般为 ajax 请求
      setTimeout(() => {
        for (let i = 0; i < 10; i++) {
          this.list.push(this.list.length + 1);
        }

        // 加载状态结束
        this.loading = false;

        // 数据全部加载完成
        if (this.list.length >= 40) {
          this.finished = true;
        }
      }, 1000);
    },
    onRefresh() {
      // 清空列表数据
      this.finished = false;

      // 重新加载数据
      // 将 loading 设置为 true，表示处于加载状态
      this.loading = true;
      this.onLoad();
    },
    async getData() {
      // console.log(localStorage.getItem("token"));

      const res = await axios({
        method: "get",
        url: "http://interview-api-t.itheima.net/h5/interview/query",
        params: {
          current: 1,
          pageSize: 10,
        },
        headers: {
          Authorization: `Bearer ${localStorage.getItem("token")}`,
        },
      });
      this.data = res.data.data.rows;
      console.log(this.data);
    },
  },
  data() {
    return {
      list: [],
      loading: true,
      finished: true,
      refreshing: false,
      data: [],
    };
  },
  created() {
    this.getData();
  },
};
</script>

<style lang="less" scoped></style>
